package cn.innosmart.aq.util;

import android.content.Context;

import java.text.DecimalFormat;
import java.util.HashMap;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AlarmConditionBean;

/**
 * Created by David on 2015/7/30.
 */
public class DeviceConstant {
    /**
     * 设备类型
     */
    public static final int FUNCTYPE_GW = 9;
    public static final int FUNCTYPE_TEMPERATURE = 11;
    public static final int FUNCTYPE_PH = 100;
    public static final int FUNCTYPE_WATERLEVER = 101;
    public static final int FUNCTYPE_SWITCH = 23;
    public static final int FUNCTYPE_FEEDER = 47;
    public static final int FUNCTYPE_METER_WATT = 22;
//    public static int FUNCTYPE_TAMPSENDOR = 31;

    //单位数组
    public static String[] SensorUnit = new String[]{
            "Unkown", "0.1℃", "0.1F", "1%", "1lx", "0.1W", "0.01V", "1mA", "0.01%", "0.01kWh"
    };
    //名字映射表
    public static HashMap<String, String> nameMap = new HashMap<String, String>();
    //区间报警  根据规则设置
    public static HashMap<String, AlarmConditionBean> alarmConditionBeanHashMap = new HashMap<>();

    /**
     * 根据单位和值 生成相应字符串
     *
     * @param senserunit 单位
     * @param value       值
     * @return
     */
    public static String caculateValue(int senserunit, double value) {
        if (senserunit == -1) {
            return String.valueOf(value);
        } else if (senserunit == 0) {
            return String.valueOf(value);
        } else {
            String sensorUnit = SensorUnit[senserunit];
            String unit = sensorUnit.replaceAll("^\\d+(\\.\\d+)?", "");
            double scale = Double.valueOf(sensorUnit.replace(unit, ""));
            double realValue = value * scale;
            DecimalFormat df = new DecimalFormat("#####0.0");
            return df.format(realValue) + "" + unit;
        }
    }

    /**
     * 根据相应单位和字符串 计算相应的值
     *
     * @param senserunity
     * @param value
     * @return
     */
    public static double convertValue(int senserunity, String value) {
        double doubleValue = Double.valueOf(value);
        if (senserunity == -1) {
            return doubleValue;
        } else if (senserunity == 0) {
            return doubleValue;
        } else if (senserunity == 1) {
            return doubleValue * 10;
        } else {
            return doubleValue;
        }
    }

    /**
     * 根据水位的值，生成相应字符串
     *
     * @param context
     * @param value
     * @return
     */
    public static String caculateWaterLever(Context context, double value) {
        if (value == 0) {
            return context.getString(R.string.water_lever_value_normal);
        } else if (value == 1) {
            return context.getString(R.string.water_lever_value_low);
        } else {
            return context.getString(R.string.water_lever_value_high);
        }
    }
}
